@ECHO OFF
REM *******************************************************************************************/
REM	CreateDefaultUserAPM.bat
REM
REM	Params: 	Server
REM			
REM
REM	Dependencies:
REM	The SQL Server utility osql must be located on the computer's path
REM
REM
REM       Date                   Author                  Description
REM ----------		------------------		----------------------------------------------------
REM 03/28/2007          Vineet Batta                    To create the default user (Administrator) in the system.
REM *******************************************************************************************/

REM -- Setting Variables
SET ProjectName=APM - Default User Creation
SET DBFileSize=200
SET DBFileGrowth=100
SET LogFileSize=50
SET LogFileGrowth=50
SET ExitCode=0

REM -- Checking Parameters
REM SET CMDFile=%~0
SET CMDFile=CreateDefaultUserAPM
IF "%~1" == "" (GOTO BadParam) ELSE (SET Server=%~1)

SET Database=APM
SET LogFile="%CMDFile%.log"


REM -- Displaying message on screen
ECHO *******************************************************************************
ECHO Executing %ProjectName% scripts on \\%Server%\%Database%
ECHO at %DATE% %TIME%.
ECHO *******************************************************************************
ECHO Connecting to \\%Server%...

REM -- Writing message to Log File
ECHO *******************************************************************************	>>  %LogFile%
ECHO Executing %ProjectName% on \\%Server%\%Database%		>> %LogFile%
ECHO at %DATE% %TIME%.					>> %LogFile%
ECHO *******************************************************************************						>> %LogFile%
ECHO Connecting to \\%Server%...																			>> %LogFile%

REM -- Connecting to SQL Server
CALL osql -S "%Server%" -d  %Database% -E -n -b -Q "PRINT 'Success'"	>> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)


ECHO -------------------------------------------------------------------	>> %LogFile%
ECHO Creating default User - APM					>> %LogFile%
ECHO ------------------------------------------------------------------- 	>> %LogFile%

CALL osql -S "%Server%" -d "%Database%" -E -n -b -i "APMFirstUser.sql"    >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

ECHO ---------------------------------------------------------------------	>> %LogFile%
ECHO Default User created Successfully.			>> %LogFile%
ECHO ---------------------------------------------------------------------	>> %LogFile%


REM -- Displaying message on screen
ECHO *******************************************************************************
ECHO  %ProjectName% - Script executed successfully on \\%Server%\%Database%
ECHO at %DATE% %TIME%.
ECHO.
ECHO Please check the log file %LogFile% for details.
ECHO *******************************************************************************

REM -- Writing message to Log File
ECHO.																										>> %LogFile%
ECHO *******************************************************************************						>> %LogFile%
ECHO %ProjectName% scripts executed successfully on \\%Server%\%Database%						>> %LogFile%
ECHO at %DATE% %TIME%.																						>> %LogFile%
ECHO *******************************************************************************						>> %LogFile%

GOTO End


REM -- Subroutine: BadParam --------------------------------------------------------
:BadParam
ECHO *******************************************************************************
ECHO Please enter valid parameter to execute this script.
ECHO.
ECHO %CMDFile% "Server" 
ECHO.
ECHO For example: %CMDFile% "SERVER" 
ECHO.
ECHO 
ECHO *******************************************************************************

SET ExitCode=1
GOTO End
REM --------------------------------------------------------------------------------


REM -- Subroutine: Crash -----------------------------------------------------------
:Crash
ECHO *******************************************************************************
ECHO Failed to execute %ProjectName% scripts on \\%Server%\%Database%
ECHO at %DATE% %TIME%.
ECHO.
ECHO Please check the log file %LogFile% for Errors!
ECHO *******************************************************************************

SET ExitCode=1
GOTO End
REM --------------------------------------------------------------------------------


REM -- Subroutine: End -------------------------------------------------------------
:End

REM --------------------------------------------------------------------------------
